package cn.bx.consumer.ctrl;

import cn.bx.consumer.service.RoleService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.example.pojo.AjaxResult;
import org.example.pojo.Page;
import org.example.pojo.User;
import org.example.pojo.Userrole;
import org.example.service.UserServiceI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
public class UserController {
    @Autowired
    private UserServiceI userService;

    @HystrixCommand(fallbackMethod = "getError", commandProperties =
            {@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2000")})
    @RequestMapping(value = "userGet", produces = "application/json;charset=utf-8")
    public AjaxResult userGet(@RequestParam("id") long id) {
        try {
            Thread.sleep((long) (2400 * Math.random()));
        } catch (InterruptedException e) {

        }
        return userService.userGet(id);
    }

    public AjaxResult getError(long id) {
        return AjaxResult.error(500, "服务器太忙，没有响应过来");
    }

    @PostMapping(value = "login", produces = "application/json;charset=utf-8")
    public AjaxResult login(@RequestParam("username") String username, @RequestParam("password") String password) {
        return userService.login(username, password);
    }

    @HystrixCommand(fallbackMethod = "searchError", commandProperties =
            {@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2000")})
    @RequestMapping(value = "userSearch", produces = "application/json;charset=utf-8")
    public Page<User> userSearch(@RequestParam(name = "page", defaultValue = "1") int pageNo, @RequestParam(name = "limit", defaultValue = "10") int limit, @RequestParam(name = "name", required = false) String name, @RequestParam(name = "start", required = false) Date start, @RequestParam(name = "end", required = false) Date end) {
        try {
            Thread.sleep((long) (2400 * Math.random()));
        } catch (InterruptedException e) {

        }
        return userService.userSearch(pageNo, limit, name, start, end);
    }

    public Page<User> searchError(int pageNo, int limit, String name, Date start, Date end) {
        Page<User> p = new Page<>();
        p.setCode(405);
        p.setMsg("服务器无响应");
        return p;
    }

    @PostMapping(value = "userAdd", produces = "application/json;charset=utf-8")
    public AjaxResult userAdd(User user) {
        return userService.userAdd(user);
    }

    @PostMapping(value = "userUpdate", produces = "application/json;charset=utf-8")
    public AjaxResult userUpdate(User user) {
        return userService.userUpdate(user);
    }

    @GetMapping(value = "userdel", produces = "application/json;charset=utf-8")
    public AjaxResult del(@RequestParam("id") long id) {
        return userService.del(id);
    }

    @GetMapping(value = "batchDeluser", produces = "application/json;charset=utf-8")
    public AjaxResult batchDel(@RequestParam("ids[]") long[] ids) {
        return userService.batchDel(ids);
    }

    @RequestMapping(value = "checkUserNameRepeat", produces = "application/json;charset=utf-8")
    public AjaxResult checkUserNameRepeat(@RequestParam("userName") String userName, @RequestParam(value = "id", required = false) Long id) {
        return userService.checkUserNameRepeat(userName, id);
    }

    //异常处理
    @ExceptionHandler
    public AjaxResult error(Exception e) {
        return AjaxResult.error("false", e);
    }

    //设置日期格式
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
        // true:允许输入空值，false:不能为空值
    }

}
